package com.han.verifycode.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class FileUtil {
    private static final String fileType = "file://";
    private static final String classpathType = "classpath://";

    /**
     * 获取文件流
     *
     * @param path
     * @return
     */
    public static InputStream getFile(String path) {
        if (path == null || path.trim().length() == 0) {
            return null;
        }

        if (path.startsWith(classpathType)) {
            String s = path.replaceFirst(classpathType, "/");
            return FileUtil.class.getResourceAsStream(s);
        } else {
            String s = path.replaceFirst(fileType, "/");
            try {
                File f = new File(s);
                if (f.exists()) {
                    return new FileInputStream(new File(s));
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        try {
            return new FileInputStream(new File(path));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

}
